#include "scan.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#include "portInfo.h"
#include "saveInfo.h"
#include "icmp.h"

#include "help.h"
#include <unistd.h>
#include <thread>
#include <string.h>

std::mutex open_ports_mutex;
std::mutex closed_ports_mutex;
std::mutex filtered_ports_mutex;
std::mutex closed_filtered_ports_mutex;
std::mutex open_filtered_ports_mutex;

std::vector<PortInfo> open_ports;
std::vector<PortInfo> closed_ports;
std::vector<PortInfo> filtered_ports;
std::vector<PortInfo> closed_filtered_ports;
std::vector<PortInfo> open_filtered_ports;

int startPort = 0;

int endPort = 0;
int main(int argc,char *argv[]){
    scanInfo scanInfo;
    if(checkArgs(argc,argv,scanInfo) == 0){
        std::cout<<"扫描程序异常退出"<<std::endl;
        return 0;
    }else if(checkArgs(argc,argv,scanInfo) == 1){
        std::cout<<"扫描程序正常退出"<<std::endl;
        return 0;
    }
    //通过参数来判断用哪个方式进行端口扫描
    if(strcmp(scanInfo.icmpScanMode,"-p") == 0){
        icmpEcho(scanInfo.ip);
    }

    checkFlag(startPort,endPort,scanInfo.portRange);

    allotThread(startPort,endPort,scanInfo.portScanMode,scanInfo.threadCount,scanInfo.ip);

    for(int i=0;i<open_filtered_ports.size();i++){
        std::cout<<"端口："<<open_filtered_ports[i].port<<"状态："<<portStatusToString(open_filtered_ports[i].state)<<std::endl;
    }
    for(int i=0;i<open_ports.size();i++){
        std::cout<<"端口："<<open_ports[i].port<<"状态："<<portStatusToString(open_ports[i].state)<<std::endl;
    }
    for(int i=0;i<closed_filtered_ports.size();i++){
        std::cout<<"端口："<<closed_filtered_ports[i].port<<"状态："<<portStatusToString(closed_filtered_ports[i].state)<<std::endl;
    }
    for(int i=0;i<closed_ports.size();i++){
        std::cout<<"端口："<<closed_ports[i].port<<"状态："<<portStatusToString(closed_ports[i].state)<<std::endl;
    }
    for(int i=0;i<filtered_ports.size();i++){
        std::cout<<"端口："<<filtered_ports[i].port<<"状态："<<portStatusToString(filtered_ports[i].state)<<std::endl;
    }
    savePortInfoToFile(open_ports, closed_ports, filtered_ports, closed_filtered_ports,open_filtered_ports,scanInfo.fileName);
    return 0;
}

